<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>资讯评论</title>
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}" media="all">
    <script th:src="@{/static/layui/layui.js}" charset="utf-8"></script>

</head>
<body>


<script type="text/javascript">
    var btnList = ['layui-btn-warm','layui-btn-danger'];
    function dict(obj,types) {
        var str = "";
        var index = 0;
        if(types === "state" ){
            if(obj.state == 0){//禁用
                index = 1;
                str = "删除";
            }
            if(obj.state == 1){
                index = 0;
                str = "未删除";
            }
        }
        var btn = "<a class='layui-btn layui-btn-xs layui-btn-radius "+btnList[index]+"'>"+str+"</a>";
        return btn;
    }

</script>

<div class="layui-form searchTable">
    <span>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input type="text" name="content" id="content" placeholder="请输入评论内容" autocomplete="off" class="layui-input datePicker">
            </div>
        </div>
        <button id="searchBtn" class="layui-btn" data-type="reload">搜索</button>
    </span>
</div>

<table class="layui-hide" id="discuss" lay-filter="discuss-query"></table>

<script type="text/html" id="operation">
    <a class="layui-btn layui-btn-danger" lay-event="del" title="删除">删除</a>
</script>

<script>
    layui.use(['jquery','table','form','laydate'],function () {
        var table = layui.table;//表单
        var layer = layui.layer;
        var $ = layui.$;
        var laydate = layui.laydate;

        table.render({
            elem:'#discuss',
            url:'/discuss/query',
            method: 'post',
            toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
            title: 'discuss',
            cols: [
                [
                    {type: 'checkbox', fixed: 'left',align:'center'},
                    {field: 'id', title:'ID', align:'center'},
                    {field: 'fid', title:'父ID', align:'center'},
                    {field: 'nid', title:'资讯id', align:'center'},
                    {field: 'uid', title:'用户id', align:'center'},
                    {field: 'content', title:'评论内容', align:'center'},
                    {field: 'state', title:'状态', align:'center', templet:function (data) {return dict(data,"state");}},
                    {field: 'likes', title:'点赞数', align:'center'},
                    {field: 'tread', title:'踩次数', align:'center'},
                    {field: 'reply', title:'回复数', align:'center'},
                    {field: 'dates', title:'评论时间', align:'center'},
                    {field: 'createdate', title:'创建时间', align:'center'},
                    {field: 'updatedate', title:'修改时间', align:'center'},
                    {fixed: 'right', title:'操作', toolbar: '#operation', align:'center'}
                ]
            ],
            page: true,//每页条数默认10
            request: {
                pageName :"currentPage",
                limitName : "pageSize"
            },
            parseData: function (res) {//res 即为原始返回的数据
                console.log(res);
                return {
                    "code": 0, //解析接口状态
                    "msg": "", //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": res.list //解析数据列表
                };
            }

        });

        //模糊查询，根据评论内容进行模糊查询
        var $  = layui.$;
        var active = {
            reload : function () {
                var vague = $("#content").val();
                var obj = {};
                if(vague != ""){
                    obj['name'] = vague;
                }
                if(vague == "" || vague == null){
                    layer.msg("请输入值");
                    return;
                }
                table.reload('discuss',{
                    page: {
                        curr : 1
                    },
                    url: '/discuss/query-vague/'+vague,
                    method : 'POST',
                    where : obj,
                    done : function (res,curr,count) {
                        this.where  = {};
                    }
                },'data');
            }
        };

        $("#searchBtn").on("click",function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : "";
        });

        //编辑
        table.on("tool(discuss-query)",function (object) {
            var data = object.data;//获取到你点击的哪一行的所有数据
            if(object.event === "del"){//编辑
                layer.confirm('确认要删除该条评论吗？', function(index){
                    //object.del();
                    var obj = {
                        id: data.id,
                        state: 0
                    };
                    $.post("/discuss/del",obj,function (data) {
                        if(data.code == 10000){
                            layer.msg("删除成功",{
                                icon: 1,
                                time: 10000
                            });
                        }else{
                            layer.msg("删除失败",{
                                icon: 1,
                                time: 20000
                            });
                        }
                    },"json");
                    layer.close(index);
                });
            }
        })
    })
</script>

</body>
</html>